<template>
  <page-header-wrapper :title="false">
    <a-card title="其他客户">
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="业务名称">
                <a-input v-model="queryParam['filter[tm_name]']" placeholder="输入业务名称"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="客户姓名">
                <a-input v-model="queryParam['filter[customer_name]']" placeholder="输入客户姓名"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="联系电话">
                  <a-input v-model="queryParam['filter[phone]']" placeholder="输入联系电话"/>
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="领取时间">
                  <a-range-picker v-model="queryParam.received_at" @change="onSubmitTime" />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="contract_content">
        <div :style="{ marginBottom: '20px' }">
          <a-button :disabled="selectedRowKeys.length === 0" size="small" type="danger" @click="batchAbandon">批量放弃</a-button>
        </div>
        <s-table
          ref="table"
          bordered
          :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
          :row-key="(record) => record.customer_id"
          :columns="customerColumns"
          :data="unpaidData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters)">
              重置
            </a-button>
          </div>
          <template slot="tm_name" slot-scope="text, record">
            {{ text }}{{ record.pt_name }}
          </template>
          <template slot="phone" slot-scope="text, record">
            {{ text }} <a-icon type="phone" theme="filled" :style="{ color: '#1890ff', cursor: 'pointer' }" @click="makePhone(record.customer_id)" />
          </template>
          <template slot="action" slot-scope="text, record">
            <a-button size="small" type="primary" style="margin-right:5px;" @click="$router.push({ name: 'CustomerDetail', params: {id: record.customer_id} })">查看</a-button>
            <a-button size="small" type="danger" style="margin-right:5px;" @click="abandon(record.customer_id)">放弃</a-button>
            <a-button size="small" type="primary" @click="$router.push({ name: 'AddContract', query: {customer_id: record.customer_id} })">做合同</a-button>
          </template>
        </s-table>
      </div>
    </a-card>
    <phone-modal v-if="modalVisible" :customer-id="customerId" @cancel="modalExit"></phone-modal>
  </page-header-wrapper>
</template>

<script>
import storage from 'store'
import { STable, PhoneModal } from '@/components'
import { batchAbandon, takeAbandon, unpaid } from '@/api/customer'
import col from '@/columns/customer'
const customerColumns = col.other

export default {
  components: {
    STable,
    PhoneModal
  },
  name: 'OtherCustomer',
  data () {
    return {
      customerColumns,
      unpaidData: parameters => {
        return unpaid(Object.assign(parameters, this.queryParam)).then(res => {
          return res.result
        })
      },
      queryParam: {},
      advanced: false,
      selectedRowKeys: [],
      modalVisible: false,
      customerId: '',
      visible: false,
      type: storage.get('Group_Type')
    }
  },
  activated () {
    this.$refs.table.refresh()
  },
  methods: {
    toggleAdvanced () {
      this.advanced = !this.advanced
    },
    makePhone (id) {
      this.customerId = id
      this.modalVisible = true
    },
    modalExit (visible) {
      this.modalVisible = visible
    },
    handleSearch (selectedKeys, confirm, dataIndex) {
      confirm()
      this.searchText = selectedKeys[0]
      this.searchedColumn = dataIndex
    },
    handleReset (clearFilters) {
      clearFilters()
    },
    onSelectChange (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    onSubmitTime (date, dateString) {
      for (let i = 0; i < dateString.length; i++) {
        if (i === 0) {
          this.queryParam['filter[received_at_start]'] = dateString[i]
        }
        if (i === 1) {
          this.queryParam['filter[received_at_end]'] = dateString[i]
        }
      }
    },
    abandon (id) {
      const app = this
      app.$confirm({
        title: '警告',
        content: '是否确定休了客户',
        onOk () {
          takeAbandon(id).then(res => {
            if (res.status === 0) {
              app.$refs.table.refresh()
            } else {
              app.$warning({
                title: res.msg
              })
            }
          })
        }
      })
    },
    batchAbandon () {
      const app = this
      app.$confirm({
        title: '警告',
        content: '是否放弃' + app.selectedRowKeys.length + '条客户资源',
        onOk () {
          const parameter = {}
          parameter['customer_ids'] = app.selectedRowKeys
          batchAbandon(parameter).then(res => {
            if (res.status === 0) {
              app.$refs.table.refresh()
              app.selectedRowKeys = []
            } else {
              app.$warning({
                title: res.msg
              })
            }
          })
        }
      })
    },
    addModal () {
      this.visible = true
    }
  }
}
</script>

<style scoped>
.ant-form-item {
  margin-bottom: 0 !important;
}
</style>
